package cn.stronglink.asset.message.mq.dam.handle;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import cn.stronglink.asset.message.mq.dam.entity.IScreenCommandFactory;
import cn.stronglink.asset.message.mq.dam.entity.RackCodeEntity;
import cn.stronglink.asset.message.mq.dam.entity.ScreenCommandEntity;
import cn.stronglink.asset.message.mq.dam.entity.dacai.UpdateArrayTextCommandFactory;
import cn.stronglink.asset.message.mq.dam.message.MQMessageOfDAM;
import cn.stronglink.asset.message.mq.topic.ReaderInfoSender;
import cn.stronglink.asset.model.DeviceType;
import cn.stronglink.asset.module.dam.entity.dev.DeviceTypeVo;
import cn.stronglink.asset.module.dam.service.DamService;
import cn.stronglink.asset.module.dam.service.IDamService;
import cn.stronglink.suite.core.util.ContextUtils;

public class OpenBindDevicePageHandle extends AbstractScreenCommandHandle{
	protected final static String TOPIC_NAME = "serviceToDam";
	protected final static String ACTION_CODE = "screenRequest";
	protected final static String SEND_ACTION_CODE= "screenCommand";
	protected IDamService damService =(DamService)ContextUtils.getBean("sensorService");
	private ReaderInfoSender topicSender = (ReaderInfoSender) ContextUtils.getBean("readerInfoSender");
	private final static short DAM_CODE_ID = 2;
	private final static short RACK_NAME_ID = 4;
	private final static short SCREEN_ID = 8;
	@Override
	protected String getCommandCode() {
		return "EEB11100070001100000";
	}

	@Override
	protected void handleMessage(MQMessageOfDAM msg) {
		RackCodeEntity rackCodeEntity = (RackCodeEntity) msg.getAwsPostdata();
		if(rackCodeEntity!=null) {
			DeviceTypeVo damDevice = damService.qryDamDevice(rackCodeEntity.getRackConverCode());
			Map<Short,String> map = new HashMap<>();
			map.put(DAM_CODE_ID,rackCodeEntity.getRackConverCode());
			if(damDevice!=null) {
				map.put(RACK_NAME_ID,damDevice.getRackName()==null?" ":damDevice.getRackName());
				map.put(DAM_CODE_ID,rackCodeEntity.getRackConverCode());
				List<DeviceType> deList = damService.qryCollectionDevice(damDevice.getAssetId());
				if(deList!=null&&deList.size()>0) {
					for(int i=0;i<2;i++) {
						if(i<deList.size()) {
							map.put((short)(2*i+6),deList.get(i).getRackConverCode()==null?" ":deList.get(i).getRackConverCode());
						}else {
							map.put((short)(2*i+6)," ");
						}
						
					}
				}else {
					map.put((short)6," ");
					map.put((short)8," ");
				}
			}else {
				map.put(RACK_NAME_ID," ");
				map.put((short)6," ");
				map.put((short)8," ");
			}
			try {
				sendDataToDam(rackCodeEntity.getRackConverCode(),map);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void sendDataToDam(String rackConverCode,Map<Short,String> map ) throws Exception {
		IScreenCommandFactory rackScreenCommandFactroy = new UpdateArrayTextCommandFactory(rackConverCode, SCREEN_ID, map);
		ScreenCommandEntity sc =  rackScreenCommandFactroy.createScreenCommand();
		
		MQMessageOfDAM message = new MQMessageOfDAM();
		message.setActioncode(SEND_ACTION_CODE);
		message.setAwsPostdata(sc);
		message.setTimestamp(System.currentTimeMillis());
		
		String json = JSON.toJSONString(message);
		topicSender.send(TOPIC_NAME,json);
		logger.info("topic:{}  发送命令:{}",TOPIC_NAME,json);
	}

}
